package com.jn.erp.wechat.mp.handler;

import com.jn.erp.wechat.core.WxMessageData;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;


/**
 * <pre>
 *  企业微信员工消息发送配置
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/25 10:58
 */
@Component
public class MpMessageHandler {

    private final WxMpService wxMpService;

    @Autowired
    public MpMessageHandler(@Qualifier("wxMpJnService") WxMpService wxMpService){
        this.wxMpService = wxMpService;
    }


    /**
     *  上课登记成功通知消息指定用户发送 携带 templateId
     */
    public Boolean ClassRegistrationMessageUser(WxMessageData wxMessageData,String templateId){
        try {
            List<WxMpTemplateData> data= Arrays.asList(
                    new WxMpTemplateData("thing13",truncateToTen(wxMessageData.getCourseName())),//课程名称
                    new WxMpTemplateData("thing7",truncateToTen(wxMessageData.getOrgName())),//上课地点
                    new WxMpTemplateData("time8",wxMessageData.getClassDate()),//上课时间
                    new WxMpTemplateData("thing6",truncateToTen(wxMessageData.getClassName()))//班级名称
            );
            WxMpTemplateMessage sendMessage=WxMpTemplateMessage.builder().templateId(templateId)
                    .data(data)
                    .toUser(wxMessageData.getOpenId()).build();
            wxMpService.getTemplateMsgService().sendTemplateMsg(sendMessage);
            return true;
        } catch (WxErrorException e) {
            System.out.println(e.getError());
            return false;
        }
    }

    /**
     *    欠费催缴通知消息指定用户发送
     *
     * @param  wxMessageData OrgName StudentName CourseName RemainClass PeriodDate
     * @return Boolean
     */
    public Boolean ArrearsTextMessageUser(WxMessageData wxMessageData,String templateId){
        try {
            List<WxMpTemplateData> data= Arrays.asList(
                    new WxMpTemplateData("thing14",truncateToTen(wxMessageData.getOrgName())),//机构名称
                    new WxMpTemplateData("thing3",truncateToTen(wxMessageData.getCourseName())),//课程名称
                    new WxMpTemplateData("thing7",truncateToTen(wxMessageData.getStudentName())),//学员姓名
                    new WxMpTemplateData("character_string9",wxMessageData.getRemainClass()),//剩余课时
                    new WxMpTemplateData("time10",wxMessageData.getPeriodDate())//有效期   DateUtils.dateTimeNow("yyyy/MM/dd HH:mm:ss")
            );
            WxMpTemplateMessage sendMessage=WxMpTemplateMessage.builder().templateId(templateId)
                    .data(data).toUser(wxMessageData.getOpenId()).build();
            wxMpService.getTemplateMsgService().sendTemplateMsg(sendMessage);
            return true;
        } catch (WxErrorException e) {
            System.out.println(e.getError());
            return false;
        }
    }

//    /**
//     *    欠费催缴通知消息指定用户发送
//     *
//     * @param  wxMessageData OrgName StudentName CourseName RemainClass PeriodDate
//     * @return Boolean
//     */
//    public Boolean ArrearsTextMessageUser(WxMessageData wxMessageData){
//            try {
//                List<WxMpTemplateData> data= Arrays.asList(
//                        new WxMpTemplateData("thing14",wxMessageData.getOrgName()),//机构名称
//                        new WxMpTemplateData("phrase2",wxMessageData.getStudentName()),//学员姓名
//                        new WxMpTemplateData("thing3",wxMessageData.getCourseName()),//课程名称
//                        new WxMpTemplateData("character_string9",wxMessageData.getRemainClass()),//剩余课时
//                        new WxMpTemplateData("time10",wxMessageData.getPeriodDate())//有效期   DateUtils.dateTimeNow("yyyy/MM/dd HH:mm:ss")
//                );
////                //配置详情小程序跳转(家长端-课时查询)
////                WxMpTemplateMessage.MiniProgram mp=new WxMpTemplateMessage.MiniProgram();
////                mp.setAppid("wxb593ce1ec505b074");
////                mp.setPagePath("pages/classhour/purchaseDetails/purchaseDetails");
////                mp.setUsePath(true);
//                WxMpTemplateMessage sendMessage=WxMpTemplateMessage.builder().templateId("JRFhti2nrzrihITvNxFhdwf2L2LgKs_QlCfDletM6JI")
////                .miniProgram(mp).url("http://weixin.qq.com/download")
//                .data(data).toUser(wxMessageData.getOpenId()).build();
//
//                wxMpService.getTemplateMsgService().sendTemplateMsg(sendMessage);
//                return true;
//            } catch (WxErrorException e) {
//                System.out.println(e.getError());
//                return false;
//            }
//        }

//    /**
//     *  上课登记成功通知消息指定用户发送
//     */
//    public Boolean ClassRegistrationMessageUser(WxMessageData wxMessageData){
//            try {
//                List<WxMpTemplateData> data= Arrays.asList(
//                        new WxMpTemplateData("thing9",wxMessageData.getOrgName()),//机构名称
//                        new WxMpTemplateData("thing13",wxMessageData.getCourseName()),//课程名称
//                        new WxMpTemplateData("time8",wxMessageData.getClassDate()),//上课时间
//                        new WxMpTemplateData("thing6",wxMessageData.getClassName())//班级名称
//                );
//                WxMpTemplateMessage sendMessage=WxMpTemplateMessage.builder().templateId("Ifhl2zZlgIHJZXWBBWC8nMjLovs61cWSYk-8gdot2gk")
//                .data(data)
//                .toUser(wxMessageData.getOpenId()).build();
//
//                wxMpService.getTemplateMsgService().sendTemplateMsg(sendMessage);
//                return true;
//            } catch (WxErrorException e) {
//                System.out.println(e.getError());
//                return false;
//            }
//        }







    //消息发送长度限制20字节
    private static String truncateToTen(String str) {
        return str.length() > 10 ? str.substring(0, 10) : str;
    }
}
